package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.SettingDao;
import com.bdqn.oa.entity.Setting;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Chenrz
 * @date 2022/7/20 20:36
 */
public class SettingDaoImpl implements SettingDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增系统设置
     *
     * @param setting
     * @return
     * @throws SQLException
     */
    @Override
    public int saveSetting(Setting setting) throws SQLException {
        String sql = "insert into `setting`(`report_limit`,`plan_limit`,`report_comment_limit`,`update_time`,`name`) " +
                " values (?,?,?,?,?);";
        Object[] params = {
                setting.getReportLimit(),setting.getPlanLimit(),setting.getReportCommentLimit(),
                setting.getUpdateTime(),setting.getName()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 删除系统设置
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteSetting(Integer id) throws SQLException {
        String sql = "delete from `setting` where id=?;";
        return queryRunner.update(DruidUtils.getConnection(),sql,id);
    }

    /**
     * 修改系统设置
     *
     * @param setting
     * @return
     * @throws SQLException
     */
    @Override
    public int updateSetting(Setting setting) throws SQLException {
        String sql = "update `setting` set `report_limit`=?,`plan_limit`=?,`report_comment_limit`=?,`update_time`=?,`name`=? where `id`=?;";
        Object[] params = {
                setting.getReportLimit(),setting.getPlanLimit(),setting.getReportCommentLimit(),
                setting.getUpdateTime(),setting.getName(),setting.getId()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 查询系统设置
     *
     * @return
     * @throws SQLException
     */
    @Override
    public Setting getSetting() throws SQLException {
        Setting setting = new Setting();
        String sql = "select `report_limit`,`plan_limit`,`report_comment_limit`,`update_time`,`name` from `setting`;";
        setting = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Setting>(Setting.class));
        return setting;
    }
}
